package com.example.note;

import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.provider.Settings;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.axdac.mixbind.TimeLine;
import com.example.note.cardlistview.RecycleViewBuilder;
import com.example.note.dao.NoteTagDAO;
import com.example.note.datahandler.ShowNoteHandler;
import com.example.note.datamaker.BuildAllNote;
import com.example.note.dbtool.MyDBHelper;
import com.example.note.util.BackupTask;
import com.example.note.util.GloableValue;
import com.example.note.util.NoteManager;
import com.example.note.util.ToastUtils;
import com.example.note.viewholder.AddNoteHolder;

public class MainActivity extends AppCompatActivity {
    private AlertDialog dialog;
    private LinearLayout linearLayout;
    BuildAllNote buildAllNote = new BuildAllNote();
    private RecycleViewBuilder recycleViewBuilder;
    int resume =0;
    String key = "";

    private void reflesh(){
        buildAllNote.setKey(key);
        recycleViewBuilder.reflesh(buildAllNote);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if(resume!=0){
            reflesh();
        }
        resume++;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        try {
            ActionBar actionBar = getSupportActionBar();
            actionBar.setTitle("note");
        } catch (Exception e) {
            e.printStackTrace();
        }


        linearLayout = findViewById(R.id.container);
        linearLayout.removeAllViews();
        recycleViewBuilder = new RecycleViewBuilder(this);
        View view = recycleViewBuilder
                .addDataMaker(buildAllNote)
                .addHandler(new ShowNoteHandler())
                .getRoot();
        linearLayout.addView(view);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final AddNoteHolder addNoalHolder = new AddNoteHolder(MainActivity.this);
                new AlertDialog.Builder(MainActivity.this)
                        .setTitle("添加笔记")
                        .setView(addNoalHolder.view)
                        .setNegativeButton("取消", null)
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                NoteManager.addNote(addNoalHolder.title.getText().toString(),addNoalHolder.content.getText().toString(),addNoalHolder.selectmode);
                                reflesh();
                            }
                        }).show();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if(id==R.id.action_shownote){
            ToastUtils.show(MainActivity.this,"过滤关键字");
            final EditText editText = new EditText(MainActivity.this);
            editText.setHint("不设条件即为全查询");
            android.app.AlertDialog dialog = new android.app.AlertDialog.Builder(MainActivity.this)
                    .setTitle("输入标签")//设置对话框的标题
                    .setView(editText)
                    .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    })
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            key = editText.getText().toString();
                            reflesh();
                        }
                    }).create();
            dialog.show();
            return true;
        }

        if(id==R.id.action_backup){
            new TimeLine.Builder().addlib(new TimeLine.timeLinePackage() {
                @TimeLine.TimeLineTick(level = 1, thread = TimeLine.TimeLineTick.BACKGROUND)
                public void doTask(TimeLine.RunningMsg msg) {
                    new BackupTask(MainActivity.this, GloableValue.DbNames).dotask("backupDatabase");
                }

                @TimeLine.TimeLineTick(level=2,thread = TimeLine.TimeLineTick.FORGROUND)
                public void after(TimeLine.RunningMsg msg) {
                    ToastUtils.show(GloableValue.appcontext, "备份成功");
                }
            }).run();
        }
        if(id==R.id.action_restore){
            new AlertDialog.Builder(this).setTitle("提示")
                    .setMessage("是否回滚数据库，请确定有数据库文件")
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            new TimeLine.Builder().addlib(new TimeLine.timeLinePackage() {


                                @TimeLine.TimeLineTick(level=1,thread = TimeLine.TimeLineTick.BACKGROUND)
                                public void doTask(TimeLine.RunningMsg msg) {
                                    new BackupTask(MainActivity.this, GloableValue.DbNames).dotask(BackupTask.COMMAND_RESTORE);
                                }

                                @TimeLine.TimeLineTick(level=2,thread = TimeLine.TimeLineTick.FORGROUND)
                                public void after(TimeLine.RunningMsg msg) {
                                    ToastUtils.show(GloableValue.appcontext,"还原成功");
                                    reflesh();
                                }
                            }).run();

                        }
                    }).setNegativeButton("取消",null).show();
        }


        if(id==R.id.action_add_tag){
            final EditText editText = new EditText(MainActivity.this);
            android.app.AlertDialog dialog = new android.app.AlertDialog.Builder(MainActivity.this)
                    .setTitle("输入标签")//设置对话框的标题
                    .setView(editText)
                    .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    })
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            NoteTagDAO noteTagDAO = (NoteTagDAO) new MyDBHelper(GloableValue.appcontext).getDAO( NoteTagDAO.class);
                            noteTagDAO.insert(editText.getText().toString());
                            dialog.dismiss();
                        }
                    }).create();
            dialog.show();
        }

        return super.onOptionsItemSelected(item);
    }
}
